<?php require_once('../Connections/webtintuc.php'); ?>
<?php
// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Load the KT_back class
require_once('../includes/nxt/KT_back.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_webtintuc = new KT_connection($webtintuc, $database_webtintuc);

//Start Restrict Access To Page
$restrict = new tNG_RestrictAccess($conn_webtintuc, "../");
//Grand Levels: Level
$restrict->addLevel("1");
$restrict->Execute();
//End Restrict Access To Page

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("Ten", true, "text", "", "", "", "Vui lòng nhập tên.");
$formValidation->addField("ThuTu", true, "numeric", "int", "", "", "Nhập đúng dạng số.");
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_webtintuc, $webtintuc);
$query_theloai = "SELECT * FROM theloai";
$theloai = mysql_query($query_theloai, $webtintuc) or die(mysql_error());
$row_theloai = mysql_fetch_assoc($theloai);
$totalRows_theloai = mysql_num_rows($theloai);

mysql_select_db($database_webtintuc, $webtintuc);
$query_Recordset1 = "SELECT TenTL, idTL FROM theloai ORDER BY TenTL";
$Recordset1 = mysql_query($query_Recordset1, $webtintuc) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

// Make an insert transaction instance
$ins_loaitin = new tNG_multipleInsert($conn_webtintuc);
$tNGs->addTransaction($ins_loaitin);
// Register triggers
$ins_loaitin->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_loaitin->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_loaitin->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$ins_loaitin->setTable("loaitin");
$ins_loaitin->addColumn("Ten", "STRING_TYPE", "POST", "Ten");
$ins_loaitin->addColumn("idTL", "NUMERIC_TYPE", "POST", "idTL");
$ins_loaitin->addColumn("ThuTu", "NUMERIC_TYPE", "POST", "ThuTu");
$ins_loaitin->addColumn("AnHien", "NUMERIC_TYPE", "POST", "AnHien");
$ins_loaitin->setPrimaryKey("idLT", "NUMERIC_TYPE");

// Make an update transaction instance
$upd_loaitin = new tNG_multipleUpdate($conn_webtintuc);
$tNGs->addTransaction($upd_loaitin);
// Register triggers
$upd_loaitin->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$upd_loaitin->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_loaitin->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$upd_loaitin->setTable("loaitin");
$upd_loaitin->addColumn("Ten", "STRING_TYPE", "POST", "Ten");
$upd_loaitin->addColumn("idTL", "NUMERIC_TYPE", "POST", "idTL");
$upd_loaitin->addColumn("ThuTu", "NUMERIC_TYPE", "POST", "ThuTu");
$upd_loaitin->addColumn("AnHien", "NUMERIC_TYPE", "POST", "AnHien");
$upd_loaitin->setPrimaryKey("idLT", "NUMERIC_TYPE", "GET", "idLT");

// Make an instance of the transaction object
$del_loaitin = new tNG_multipleDelete($conn_webtintuc);
$tNGs->addTransaction($del_loaitin);
// Register triggers
$del_loaitin->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Delete1");
$del_loaitin->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$del_loaitin->setTable("loaitin");
$del_loaitin->setPrimaryKey("idLT", "NUMERIC_TYPE", "GET", "idLT");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rsloaitin = $tNGs->getRecordset("loaitin");
$row_rsloaitin = mysql_fetch_assoc($rsloaitin);
$totalRows_rsloaitin = mysql_num_rows($rsloaitin);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="main.css" rel="stylesheet" type="text/css" />
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
<script src="../includes/nxt/scripts/form.js" type="text/javascript"></script>
<script src="../includes/nxt/scripts/form.js.php" type="text/javascript"></script>
<script type="text/javascript">
$NXT_FORM_SETTINGS = {
  duplicate_buttons: true,
  show_as_grid: true,
  merge_down_value: true
}
</script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td id="banner" colspan="2">TIN TỨC ONLINE. TRANG QUẢN TRỊ</td>
  </tr>
  <tr>
    <td width="150" valign="top" id="left"><table id="menu" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><a href="index.php">Trang chủ</a></td>
      </tr>
      <tr>
        <td><a href="listTheLoai.php"> thể loại</a></td>
      </tr>
      <tr>
        <td><a href="listLoaiTin.php"> loại tin</a></td>
      </tr>
      <tr>
        <td><a href="listTin.php"> Tin</a><a href="listUsers.php">Users</a></td>
      </tr>
      <tr>
        <td><a href="../index.php"> Thoát</a></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
    <td valign="top" id="right">&nbsp;
      <?php
	echo $tNGs->getErrorMsg();
?>
      <div class="KT_tng">
        <h1>
          <?php 
// Show IF Conditional region1 
if (@$_GET['idLT'] == "") {
?>
            <?php echo NXT_getResource("Insert_FH"); ?>
            <?php 
// else Conditional region1
} else { ?>
            <?php echo NXT_getResource("Update_FH"); ?>
            <?php } 
// endif Conditional region1
?>
          Loaitin </h1>
        <div class="KT_tngform">
          <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
            <?php $cnt1 = 0; ?>
            <?php do { ?>
              <?php $cnt1++; ?>
              <?php 
// Show IF Conditional region1 
if (@$totalRows_rsloaitin > 1) {
?>
                <h2><?php echo NXT_getResource("Record_FH"); ?> <?php echo $cnt1; ?></h2>
                <?php } 
// endif Conditional region1
?>
              <table cellpadding="2" cellspacing="0" class="KT_tngtable">
                <tr>
                  <td class="KT_th"><label for="Ten_<?php echo $cnt1; ?>">Ten:</label></td>
                  <td><input type="text" name="Ten_<?php echo $cnt1; ?>" id="Ten_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rsloaitin['Ten']); ?>" size="32" maxlength="100" />
                      <?php echo $tNGs->displayFieldHint("Ten");?> <?php echo $tNGs->displayFieldError("loaitin", "Ten", $cnt1); ?> </td>
                </tr>
                <tr>
                  <td class="KT_th"><label for="idTL_<?php echo $cnt1; ?>">idTL:</label></td>
                  <td><select name="idTL_<?php echo $cnt1; ?>" id="idTL_<?php echo $cnt1; ?>">
                      <option value=""><?php echo NXT_getResource("Select one..."); ?></option>
                      <?php 
do {  
?>
                      <option value="<?php echo $row_theloai['idTL']?>"<?php if (!(strcmp($row_theloai['idTL'], $row_rsloaitin['idTL']))) {echo "SELECTED";} ?>><?php echo $row_theloai['TenTL']?></option>
                      <?php
} while ($row_theloai = mysql_fetch_assoc($theloai));
  $rows = mysql_num_rows($theloai);
  if($rows > 0) {
      mysql_data_seek($theloai, 0);
	  $row_theloai = mysql_fetch_assoc($theloai);
  }
?>
                    </select>
                      <?php echo $tNGs->displayFieldError("loaitin", "idTL", $cnt1); ?> </td>
                </tr>
                <tr>
                  <td class="KT_th"><label for="ThuTu_<?php echo $cnt1; ?>">ThuTu:</label></td>
                  <td><input type="text" name="ThuTu_<?php echo $cnt1; ?>" id="ThuTu_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rsloaitin['ThuTu']); ?>" size="7" />
                      <?php echo $tNGs->displayFieldHint("ThuTu");?> <?php echo $tNGs->displayFieldError("loaitin", "ThuTu", $cnt1); ?> </td>
                </tr>
                <tr>
                  <td class="KT_th"><label for="AnHien_<?php echo $cnt1; ?>_1">AnHien:</label></td>
                  <td><div>
                      <input <?php if (!(strcmp(KT_escapeAttribute($row_rsloaitin['AnHien']),"1"))) {echo "CHECKED";} ?> type="radio" name="AnHien_<?php echo $cnt1; ?>" id="AnHien_<?php echo $cnt1; ?>_1" value="1" />
                      <label for="AnHien_<?php echo $cnt1; ?>_1">Hiện</label>
                    </div>
                      <div>
                        <input <?php if (!(strcmp(KT_escapeAttribute($row_rsloaitin['AnHien']),"0"))) {echo "CHECKED";} ?> type="radio" name="AnHien_<?php echo $cnt1; ?>" id="AnHien_<?php echo $cnt1; ?>_2" value="0" />
                        <label for="AnHien_<?php echo $cnt1; ?>_2">Ẩn</label>
                      </div>
                    <?php echo $tNGs->displayFieldError("loaitin", "AnHien", $cnt1); ?> </td>
                </tr>
              </table>
              <input type="hidden" name="kt_pk_loaitin_<?php echo $cnt1; ?>" class="id_field" value="<?php echo KT_escapeAttribute($row_rsloaitin['kt_pk_loaitin']); ?>" />
              <?php } while ($row_rsloaitin = mysql_fetch_assoc($rsloaitin)); ?>
            <div class="KT_bottombuttons">
              <div>
                <?php 
      // Show IF Conditional region1
      if (@$_GET['idLT'] == "") {
      ?>
                  <input type="submit" name="KT_Insert1" id="KT_Insert1" value="<?php echo NXT_getResource("Insert_FB"); ?>" />
                  <?php 
      // else Conditional region1
      } else { ?>
                  <div class="KT_operations">
                    <input type="submit" name="KT_Insert1" value="<?php echo NXT_getResource("Insert as new_FB"); ?>" onclick="nxt_form_insertasnew(this, 'idLT')" />
                  </div>
                  <input type="submit" name="KT_Update1" value="<?php echo NXT_getResource("Update_FB"); ?>" />
                  <input type="submit" name="KT_Delete1" value="<?php echo NXT_getResource("Delete_FB"); ?>" onclick="return confirm('<?php echo NXT_getResource("Are you sure?"); ?>');" />
                  <?php }
      // endif Conditional region1
      ?>
                <input type="button" name="KT_Cancel1" value="<?php echo NXT_getResource("Cancel_FB"); ?>" onclick="return UNI_navigateCancel(event, '../includes/nxt/back.php')" />
              </div>
            </div>
          </form>
        </div>
        <br class="clearfixplain" />
      </div>
    <p>&nbsp;</p></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($theloai);

mysql_free_result($Recordset1);
?>
